﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SaleOnlineSystem.Admin.Controls
{
    public partial class EditCategory : System.Web.UI.UserControl
    {
        public string catename;
        public string image;
        public string description;
        public string showcateleft;
        public int intactivetop;
        public int intactiveleft;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Request.QueryString["catid"] != null)
                {
                    int cateid = Convert.ToInt32(Request.QueryString["catid"]);
                    DataTable dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Category", "*", "Category_ID1", cateid, "selectwhereid");
                    catename = dt.Rows[0]["Category_Name"].ToString();
                    image = dt.Rows[0]["Category_Image"].ToString();
                    description = dt.Rows[0]["Category_Description"].ToString();
                    showcateleft = dt.Rows[0]["Category_ActiveLeft"].ToString();                 
                    if (showcateleft == "1")
                    {
                        showcateleft = "Có";
                    }
                    else
                    {
                        showcateleft = "Không";
                    }

                    txtCateName.Text = catename;
                    CKEditorMota.Text = description;
                    ddlActiveLeft.Text = showcateleft;
                }
            }
            catch (Exception)
            {
                Response.Redirect("/error.htm");
            }         
        }

        protected void btnUpdateCate_Click(object sender, EventArgs e)
        {          
                int cateid = Convert.ToInt32(Request.QueryString["catid"]);
                string catename = txtCateName.Text;
                string description = CKEditorMota.Text;
                string activeleft = ddlActiveLeft.Text;
                string filePath = null;
                string image = "";
                
                if (activeleft == "Có")
                {
                    intactiveleft = 1;
                }
                else
                {
                    intactiveleft = 0;
                }
                DataTable dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Category", "*", "Category_ID1", cateid, "selectwhereid");
                image = dt.Rows[0]["Category_Image"].ToString();

                if (catename != "")
                {
                    if (fulImage.HasFile)
                    {
                        if (LIB.Validate.CheckFileType(fulImage.FileName))
                        {
                            string link = fulImage.FileName;
                            LIB.Validate saveimage = new LIB.Validate();
                            saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Images/Image-Category/"), link, 250, fulImage.PostedFile.InputStream);
                            filePath = "/Images/Image-Category/" + fulImage.FileName;
                            if (image == "")
                            {
                                DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[4];
                                cl[0] = new DAL.DHelpers.Column("Category_Name", catename, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                cl[1] = new DAL.DHelpers.Column("Category_Image", filePath, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                cl[2] = new DAL.DHelpers.Column("Category_Description", description, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                                cl[3] = new DAL.DHelpers.Column("Category_ActiveLeft", intactiveleft.ToString(), DAL.DHelpers.DataType.DataTypeSql.Int);

                                string[] colWhere = new string[1];
                                colWhere[0] = "Category_ID1";

                                object[] colValue = new object[1];
                                colValue[0] = cateid;

                                DAL.DCollector.DBCollector.Instance.Update("tbl_Category", cl, colWhere, colValue);
                                Response.Redirect("/Admin/Index.aspx?panel=2&sub=2&catid=" + cateid);
                            }
                            else
                            {
                                File.Delete(MapPath(image));
                                DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[4];
                                cl[0] = new DAL.DHelpers.Column("Category_Name", catename, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                cl[1] = new DAL.DHelpers.Column("Category_Image", filePath, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                cl[2] = new DAL.DHelpers.Column("Category_Description", description, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                                cl[3] = new DAL.DHelpers.Column("Category_ActiveLeft", intactiveleft.ToString(), DAL.DHelpers.DataType.DataTypeSql.Int);

                                string[] colWhere = new string[1];
                                colWhere[0] = "Category_ID1";

                                object[] colValue = new object[1];
                                colValue[0] = cateid;

                                DAL.DCollector.DBCollector.Instance.Update("tbl_Category", cl, colWhere, colValue);
                                Response.Redirect("/Admin/Index.aspx?panel=2&sub=2&catid=" + cateid);
                            }
                        }
                        else
                        {
                            Response.Write("<script>alert('Chỉ có thể upload file(.jpg,.png,.jpeg)')</script>");
                        }
                    }
                    else
                    {
                        DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[3];
                        cl[0] = new DAL.DHelpers.Column("Category_Name", catename, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                        cl[1] = new DAL.DHelpers.Column("Category_Description", description, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                        cl[2] = new DAL.DHelpers.Column("Category_ActiveLeft", intactiveleft.ToString(), DAL.DHelpers.DataType.DataTypeSql.Int);

                        string[] colWhere = new string[1];
                        colWhere[0] = "Category_ID1";

                        object[] colValue = new object[1];
                        colValue[0] = cateid;

                        DAL.DCollector.DBCollector.Instance.Update("tbl_Category", cl, colWhere, colValue);
                        Response.Redirect("/Admin/Index.aspx?panel=2&sub=2&catid="+ cateid);
                    }
                }
                else
                {
                    Response.Write("<script>alert('Vui lòng nhập tên danh mục...')</script>");
                }                        
        }
    }
}